;*****************************************************************************************************
;+
; NAME:
;
;      IGL_ViewContents
;
; PURPOSE:
;
;      This routine is a slightly changed copy of the CATVIEWCONTENTS
;      procedure which is part of the Catalyst Library. The only
;      difference is the addition of the FILTER keyword. It is
;      used internally by the Interactive Graphics Library and should
;      never be called directly by the user. See the Catalyst library
;      documentation for more details.
;
;
; CALLING SEQUENCE:
;
;      IGL_ViewContents, object
;
; ARGUMENTS:
;
;      object:       The object reference of the object whose contents you wish to view.
;                 
; KEYWORD PARAMETERS:
;
;      FILTER:       Set this keyword to an object class name. Only
;                    objects of the given class (including
;                    derived classes) will be displayed in the
;                    PROPERTYPANEL object.
;
;
;      _EXTRA:       Any keyword accepted by the CATVIEWCONTENTS procedure.
;
; PACKAGE:
;
;    Interactive Graphics Library (IGL)
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
;
; MODIFICATION HISTORY:
;
;     Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
PRO IGL_ViewContents, object, $
                      FLAG=flag, $
                      GROUP_LEADER=group_leader, $
                      PARENT=parent, $
                      TITLE=title, $
                      XSIZE=xsize, $
                      YSIZE=ysize, $
                      TREE=tree, $
                      FILTER=filter

   @cat_pro_error_handler

   IF Obj_Valid(object) THEN BEGIN

      ; If this is the first time this is being called.
      IF N_Elements(flag) EQ 0 THEN BEGIN

         IF N_Elements(xsize) EQ 0 THEN xsize = 600
         IF N_Elements(ysize) EQ 0 THEN ysize = 600
         IF N_Elements(title) EQ 0 THEN title = 'Contents of ' + object->GetName()
         IF (n_elements(filter) EQ 0) THEN filter = 'CatAtom'
         IF (filter EQ '') THEN filter = 'CatAtom'

         ; Do you need to create a base widget?
         IF N_Elements(parent) EQ 0 THEN BEGIN
            parent = Obj_New('VIEWCONTENTS', Column=1, Title=title, $
                  GROUP_LEADER=group_leader )
            createdTLB = 1
         ENDIF ELSE createdTLB = 0

         ; Create the root node of the tree.
         rootnode = Obj_New('TreeWidget', parent, /Folder, /Expanded, Value=Obj_Class(object), $
             /Top, YSize=ysize, XSize=xsize, NAME='Root Node')

         ; Create the first tree folder, which is the parent of the tree.
         tree = Obj_New('TreeWidget', rootnode, /Folder, /Expanded, $
               Value=object->GetName(), UVALUE=object)

         IF createdTLB THEN parent -> Draw, /Center

      ENDIF

      ; Does this object have children?
      children = object -> Get(/All, Count=count)
      
      IF count EQ 0 THEN RETURN

      ; If there are children, traverse each child's container for leafs.
      FOR j=0, count-1 DO BEGIN
         child = children[j]
         IF Obj_Valid(child) THEN BEGIN

            value = child->GetName()
            targets = child -> Get(/all,count=nValid,ISA=filter)
            IF (obj_isa(child,filter)) THEN BEGIN
               IF (nValid GT 0) THEN BEGIN
                  newTree = Obj_New('TreeWidget', tree, /Folder, /Expanded, Value=value, UValue=child)
               ENDIF ELSE BEGIN
                  newTree = Obj_New('TreeWidget', tree, Value=value, UValue=child)
               ENDELSE
            ENDIF ELSE newTree = tree
            IF (nValid GT 0) THEN IGL_ViewContents, child, FLAG=1, TREE=newTree, filter=filter
         ENDIF
      ENDFOR
   ENDIF ELSE Message, 'Must pass a valid object reference.'

END
